<?php

class ShowController extends Controller
{
    public function actionIndex()
    {
        /*$sort = new CSort();
        $sort->attributes = array(
            //'defaultOrder'=>'t.create_time DESC',
            'id'=>array(
                'asc'=>'id ASC',
                'desc'=>'id DESC',
            ),
            'time_create'=>array(
                'asc'=>'time_create ASC',
                'desc'=>'time_create desc',
            ),
            'money'=>array(
                'asc'=>'money asc',
                'desc'=>'money desc',
            ),
        );*/
        if(isset($_GET['id'])){
            $dataProvider=new CActiveDataProvider('Service',array( // 
                'criteria'=>array(
                    'condition'=>'servicetype='.$_GET['id'],  
                    'order'=>'id DESC',
                ),
                'pagination'=>array(
                    'pageSize'=>5,
                ),
            ));
        }else{
             $dataProvider=new CActiveDataProvider('Service',array( // 
                'criteria' =>array(
                    'order'=>'id DESC',
                ),
                'pagination'=>array(
                    'pageSize'=>5,
                    
                ),
                //'sort' =>$sort,
            ));
        }
        
        $this->render('index',array(
            'dataProvider'=>$dataProvider,
        ));

    }
    
    public function actionUser(){
        $criteria = new CDbCriteria;
        $criteria->order = 'id DESC';
        $criteria->condition='user_id='.$_GET['id'];
        
        $count = Service::model()->count($criteria);
        $pages = new CPagination($count);
        $pages->pageSize = 5;
        $pages->applyLimit($criteria);
        
        $model = Service::model()->findAll($criteria);
        $this->render('user',array('model'=>$model,
                                            'pages'=>$pages));
    }
	
	
     public function actionTag($tag)
     {
        try{
            $criteria = new CDbCriteria;
            $criteria->order= "time_create DESC";
            $criteria->compare('keyword',$tag,true);
            
            $dataProvider=new CActiveDataProvider('Service',
                array(
                    'criteria'=>$criteria,
                    'pagination'=>array(
                        'pageSize'=>5,
                    ),
                )
            );
            if($dataProvider === null){
                throw new CHttpException(404,'The requested page does not exist.');
            }else{
                $this->render('tag',array('dataProvider'=>$dataProvider));
                Yii::app()->end();
            }
        }catch(Exception $ex){
            throw new CHttpException(404,'The requested page does not exist.');
        }
    }
    
    public function actionJob(){
        $arrTag = explode(",",Services::getTypeJob($_GET['id']));
        $criteria = new CDbCriteria;
        $criteria->order ="id DESC";
        //
        $criteria->addSearchCondition('servicetype',Services::getTypeJob($_GET['id']), true, 'AND');
        //$criteria->condition = 'id !='.$_GET['id'];
        $criteria->addSearchCondition('id',$_GET['id'], false, 'AND');
        foreach($arrTag as $tag){
                 $criteria->addSearchCondition('keyword',$tag, true, 'OR');
        }
        $criteria->limit = 5;
        $count = Service::model()->count($criteria);
        $pages = new CPagination($count);
        $pages->pageSize = 5;
        $pages->applyLimit($criteria);
        
        $model = Service::model()->findAll($criteria);
        $this->render('job',array('model'=>$model,'pages'=>$pages));
    }
    
    
}